package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Student;
import com.example.entity.vo.StudentVO;
import com.example.mapper.StudentMapper;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    /**
     * 失效情况一:
     */
    @Transactional
    @Override
    public void testInvalidDS(String name) {
        Student student = new Student().setName(name).setAge(86);
        boolean save = this.save(student);
        System.out.println("=== save success: " + save);
    }

    @Override
    public Page<StudentVO> getStudentList() {
        QueryWrapper<StudentVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s.parent_id", 73100817310081L);
        return baseMapper.getStudentList(queryWrapper, new Page<StudentVO>(1, 10));
    }

    /**
     * 实际上, Mapper 的 @DS 不生效仅仅是因为 saveBatch 没有执行到 Mapper 层的办法？
     */
    @Override
    public boolean testBatch() {
        List<Student> list = this.lambdaQuery().list();
        System.out.println(list);

        Student student1 = new Student().setName("tom").setAge(10);
        Student student2 = new Student().setName("tony").setAge(20);

        List<Student> studentList = Arrays.asList(student1, student2);
        // 这里的事务不会生效的吧
        boolean success = this.saveBatch(studentList);
        log.info("@DS + 批量保存结果：" + success);
        return success;
    }

    @Override
    @Transactional
    public int testDSAdd() {
        int res = this.getBaseMapper().insert(new Student().setName("tom1").setAge(19));
        return res;
    }

}
